package com.bnz.servlet;

import com.alibaba.fastjson.JSON;
import com.bnz.pojo.PageResult;
import com.bnz.pojo.R;
import com.bnz.pojo.Student;
import com.bnz.service.StudentService;
import com.github.pagehelper.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/student")
public class StudentServlet extends HttpServlet {
    @Autowired
    private StudentService studentService;
    @Override
    public void init() throws ServletException {
        //在servlet中拿到spring容器。
        WebApplicationContext webApplicationContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
        //得到studentService
        studentService = webApplicationContext.getBean(StudentService.class);

    }
    //请求调用
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //拿到参数方法名
        String cmd = req.getParameter("cmd");
        //调用方法
        if (StringUtils.isNotBlank(cmd)){
            if ("list".equals(cmd)){
                list(req,resp);
            }
            if("findByPage".equals(cmd)){
                findByPage(req,resp);
            }
            if ("search".equals(cmd)){
                search(req,resp);
            }
        }

    }

    //条件分页
    private void search(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //拿到参数
        Integer page = new Integer(req.getParameter("page"));
        Integer pageSize = new Integer(req.getParameter("pageSize"));
        Student student = JSON.parseObject(req.getParameter("student"), Student.class);
        //开始分页
        PageResult<Student> pr = studentService.search(page,pageSize,student);
        //提交分页数据
        resp.getWriter().println(JSON.toJSONString(R.ok().data("pr",pr)));
        //刷新
        resp.getWriter().flush();
    }

    /**
     * 分页
     * @param req
     * @param resp
     */
    private void findByPage(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //拿到参数
        Integer page = new Integer(req.getParameter("page"));
        Integer pageSize = new Integer(req.getParameter("pageSize"));
        //开始分页
        PageResult<Student> pr = studentService.findByPage(page,pageSize);
        //提交分页数据
        resp.getWriter().println(JSON.toJSONString(R.ok().data("pr",pr)));
        //刷新
        resp.getWriter().flush();
    }

    //所有学生信息
    private void list(HttpServletRequest req, HttpServletResponse resp) throws IOException {
       List<Student> students =  studentService.findAll();
       //将学生对象转化为json对象。
        R data = R.ok().data("data", students);
        resp.getWriter().println(JSON.toJSONString(data));
        //刷新页面
        resp.getWriter().flush();
    }
}
